{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture\n",
    "!pip install --upgrade wandb\n",
    "!wandb login 3da7a23df9fd940d985adf808de2b09ceb85f15b\n",
    "\n",
    "import wandb\n",
    "wandb.init(project=\"global-wheat-detection\", name='FasterRCNN with ResNet101 backbone: fold4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
    "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5"
   },
   "outputs": [],
   "source": [
    "%%capture\n",
    "!pip install cython\n",
    "!pip install 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'\n",
    "!cp /kaggle/input/rcnnutilswithwandb/engine.py .\n",
    "!cp /kaggle/input/rcnnutilswithwandb/utils.py .\n",
    "!cp /kaggle/input/rcnnutilswithwandb/coco_eval.py .\n",
    "!cp /kaggle/input/rcnnutilswithwandb/coco_utils.py .\n",
    "!cp /kaggle/input/rcnnutilswithwandb/transforms.py ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
    "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import ast\n",
    "import numpy as np # linear algebra\n",
    "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
    "\n",
    "import cv2\n",
    "\n",
    "import torch\n",
    "from PIL import Image\n",
    "from tqdm.auto import tqdm\n",
    "\n",
    "import albumentations\n",
    "from albumentations.pytorch.transforms import ToTensorV2\n",
    "\n",
    "from torch import nn\n",
    "import torchvision\n",
    "import torch.utils.data as data_utils\n",
    "from torch.utils.data import DataLoader, Dataset\n",
    "from torchvision.models.detection.faster_rcnn import FastRCNNPredictor\n",
    "from torchvision.models.detection import FasterRCNN\n",
    "from torchvision.models.detection.rpn import AnchorGenerator\n",
    "\n",
    "from matplotlib import pyplot as plt\n",
    "import matplotlib.patches as patches\n",
    "\n",
    "import utils\n",
    "from engine import train_one_epoch, evaluate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Constants\n",
    "TEST_DIR = '/kaggle/input/global-wheat-detection/test'\n",
    "BASE_DIR = '/kaggle/input/gwdaugmented/train'\n",
    "BATCH_SIZE = 2\n",
    "DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n",
    "# our dataset has two classes only - background and wheat heads\n",
    "N_CLASSES = 2\n",
    "N_EPOCHS = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>image_id</th>\n",
       "      <th>x_min</th>\n",
       "      <th>y_min</th>\n",
       "      <th>x_max</th>\n",
       "      <th>y_max</th>\n",
       "      <th>width</th>\n",
       "      <th>height</th>\n",
       "      <th>area</th>\n",
       "      <th>source</th>\n",
       "      <th>kfold</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>834.0</td>\n",
       "      <td>222.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>258.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>226.0</td>\n",
       "      <td>548.0</td>\n",
       "      <td>356.0</td>\n",
       "      <td>606.0</td>\n",
       "      <td>130.0</td>\n",
       "      <td>58.0</td>\n",
       "      <td>7540.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>377.0</td>\n",
       "      <td>504.0</td>\n",
       "      <td>451.0</td>\n",
       "      <td>664.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>160.0</td>\n",
       "      <td>11840.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>834.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>943.0</td>\n",
       "      <td>202.0</td>\n",
       "      <td>109.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>11663.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>26.0</td>\n",
       "      <td>144.0</td>\n",
       "      <td>150.0</td>\n",
       "      <td>261.0</td>\n",
       "      <td>124.0</td>\n",
       "      <td>117.0</td>\n",
       "      <td>14508.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295533</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>876.0</td>\n",
       "      <td>619.0</td>\n",
       "      <td>960.0</td>\n",
       "      <td>714.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>7980.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295534</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>625.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>732.0</td>\n",
       "      <td>631.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>8774.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295535</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>749.0</td>\n",
       "      <td>228.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>299.0</td>\n",
       "      <td>141.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>10011.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295536</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>410.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>594.0</td>\n",
       "      <td>92.0</td>\n",
       "      <td>184.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>14536.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295537</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>55.0</td>\n",
       "      <td>740.0</td>\n",
       "      <td>149.0</td>\n",
       "      <td>801.0</td>\n",
       "      <td>94.0</td>\n",
       "      <td>61.0</td>\n",
       "      <td>5734.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>295538 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             image_id  x_min  y_min  x_max  y_max  width  height     area  \\\n",
       "0           b6ab77fd7  834.0  222.0  890.0  258.0   56.0    36.0   2016.0   \n",
       "1           b6ab77fd7  226.0  548.0  356.0  606.0  130.0    58.0   7540.0   \n",
       "2           b6ab77fd7  377.0  504.0  451.0  664.0   74.0   160.0  11840.0   \n",
       "3           b6ab77fd7  834.0   95.0  943.0  202.0  109.0   107.0  11663.0   \n",
       "4           b6ab77fd7   26.0  144.0  150.0  261.0  124.0   117.0  14508.0   \n",
       "...               ...    ...    ...    ...    ...    ...     ...      ...   \n",
       "295533  5e0747034_aug  876.0  619.0  960.0  714.0   84.0    95.0   7980.0   \n",
       "295534  5e0747034_aug  625.0  549.0  732.0  631.0  107.0    82.0   8774.0   \n",
       "295535  5e0747034_aug  749.0  228.0  890.0  299.0  141.0    71.0  10011.0   \n",
       "295536  5e0747034_aug  410.0   13.0  594.0   92.0  184.0    79.0  14536.0   \n",
       "295537  5e0747034_aug   55.0  740.0  149.0  801.0   94.0    61.0   5734.0   \n",
       "\n",
       "           source  kfold  \n",
       "0         usask_1      2  \n",
       "1         usask_1      2  \n",
       "2         usask_1      2  \n",
       "3         usask_1      2  \n",
       "4         usask_1      2  \n",
       "...           ...    ...  \n",
       "295533  arvalis_2      1  \n",
       "295534  arvalis_2      1  \n",
       "295535  arvalis_2      1  \n",
       "295536  arvalis_2      1  \n",
       "295537  arvalis_2      1  \n",
       "\n",
       "[295538 rows x 10 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df = pd.read_csv(os.path.join('/kaggle/input/gwdaugmented/', 'train.csv'))\n",
    "train_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>image_id</th>\n",
       "      <th>x_min</th>\n",
       "      <th>y_min</th>\n",
       "      <th>x_max</th>\n",
       "      <th>y_max</th>\n",
       "      <th>width</th>\n",
       "      <th>height</th>\n",
       "      <th>area</th>\n",
       "      <th>source</th>\n",
       "      <th>kfold</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>834.0</td>\n",
       "      <td>222.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>258.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>226.0</td>\n",
       "      <td>548.0</td>\n",
       "      <td>356.0</td>\n",
       "      <td>606.0</td>\n",
       "      <td>130.0</td>\n",
       "      <td>58.0</td>\n",
       "      <td>7540.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>377.0</td>\n",
       "      <td>504.0</td>\n",
       "      <td>451.0</td>\n",
       "      <td>664.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>160.0</td>\n",
       "      <td>11840.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>834.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>943.0</td>\n",
       "      <td>202.0</td>\n",
       "      <td>109.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>11663.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b6ab77fd7</td>\n",
       "      <td>26.0</td>\n",
       "      <td>144.0</td>\n",
       "      <td>150.0</td>\n",
       "      <td>261.0</td>\n",
       "      <td>124.0</td>\n",
       "      <td>117.0</td>\n",
       "      <td>14508.0</td>\n",
       "      <td>usask_1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295533</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>876.0</td>\n",
       "      <td>619.0</td>\n",
       "      <td>960.0</td>\n",
       "      <td>714.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>7980.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295534</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>625.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>732.0</td>\n",
       "      <td>631.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>8774.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295535</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>749.0</td>\n",
       "      <td>228.0</td>\n",
       "      <td>890.0</td>\n",
       "      <td>299.0</td>\n",
       "      <td>141.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>10011.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295536</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>410.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>594.0</td>\n",
       "      <td>92.0</td>\n",
       "      <td>184.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>14536.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295537</th>\n",
       "      <td>5e0747034_aug</td>\n",
       "      <td>55.0</td>\n",
       "      <td>740.0</td>\n",
       "      <td>149.0</td>\n",
       "      <td>801.0</td>\n",
       "      <td>94.0</td>\n",
       "      <td>61.0</td>\n",
       "      <td>5734.0</td>\n",
       "      <td>arvalis_2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>295538 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             image_id  x_min  y_min  x_max  y_max  width  height     area  \\\n",
       "0           b6ab77fd7  834.0  222.0  890.0  258.0   56.0    36.0   2016.0   \n",
       "1           b6ab77fd7  226.0  548.0  356.0  606.0  130.0    58.0   7540.0   \n",
       "2           b6ab77fd7  377.0  504.0  451.0  664.0   74.0   160.0  11840.0   \n",
       "3           b6ab77fd7  834.0   95.0  943.0  202.0  109.0   107.0  11663.0   \n",
       "4           b6ab77fd7   26.0  144.0  150.0  261.0  124.0   117.0  14508.0   \n",
       "...               ...    ...    ...    ...    ...    ...     ...      ...   \n",
       "295533  5e0747034_aug  876.0  619.0  960.0  714.0   84.0    95.0   7980.0   \n",
       "295534  5e0747034_aug  625.0  549.0  732.0  631.0  107.0    82.0   8774.0   \n",
       "295535  5e0747034_aug  749.0  228.0  890.0  299.0  141.0    71.0  10011.0   \n",
       "295536  5e0747034_aug  410.0   13.0  594.0   92.0  184.0    79.0  14536.0   \n",
       "295537  5e0747034_aug   55.0  740.0  149.0  801.0   94.0    61.0   5734.0   \n",
       "\n",
       "           source  kfold  \n",
       "0         usask_1      2  \n",
       "1         usask_1      2  \n",
       "2         usask_1      2  \n",
       "3         usask_1      2  \n",
       "4         usask_1      2  \n",
       "...           ...    ...  \n",
       "295533  arvalis_2      1  \n",
       "295534  arvalis_2      1  \n",
       "295535  arvalis_2      1  \n",
       "295536  arvalis_2      1  \n",
       "295537  arvalis_2      1  \n",
       "\n",
       "[295538 rows x 10 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "class WheatDataset(Dataset):\n",
    "    \n",
    "    def __init__(self, df, folds, transforms=None):\n",
    "        self.df = df[df.kfold.isin(folds)].reset_index(drop=True)\n",
    "        self.image_ids = self.df['image_id'].unique()\n",
    "        self.transforms = transforms\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.image_ids)\n",
    "    \n",
    "    def __getitem__(self, index):\n",
    "        image_id = self.image_ids[index]\n",
    "        image = cv2.imread(os.path.join(BASE_DIR, 'train', f'{image_id}.jpg'), cv2.IMREAD_COLOR)\n",
    "        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n",
    "        image /= 255.0\n",
    "\n",
    "        # Convert from NHWC to NCHW as pytorch expects images in NCHW format\n",
    "        image = np.transpose(image, (2, 0, 1))\n",
    "        image = torch.from_numpy(image)\n",
    "        \n",
    "        # Get bbox coordinates for each wheat head(s)\n",
    "        bboxes_df = self.df[self.df['image_id'] == image_id]\n",
    "        boxes, areas = [], []\n",
    "        n_objects = len(bboxes_df)  # Number of wheat heads in the given image\n",
    "\n",
    "        for i in range(n_objects):\n",
    "            x_min = bboxes_df.iloc[i]['x_min']\n",
    "            x_max = bboxes_df.iloc[i]['x_max']\n",
    "            y_min = bboxes_df.iloc[i]['y_min']\n",
    "            y_max = bboxes_df.iloc[i]['y_max']\n",
    "\n",
    "            boxes.append([x_min, y_min, x_max, y_max])\n",
    "            areas.append(bboxes_df.iloc[i]['area'])\n",
    "\n",
    "        boxes = torch.as_tensor(boxes, dtype=torch.int64)\n",
    "        \n",
    "        # Get the labels. We have only one class (wheat head)\n",
    "        labels = torch.ones((n_objects, ), dtype=torch.int64)\n",
    "        \n",
    "        areas = torch.as_tensor(areas)\n",
    "        \n",
    "        # suppose all instances are not crowd\n",
    "        iscrowd = torch.zeros((n_objects, ), dtype=torch.int64)\n",
    "        \n",
    "        target = {\n",
    "            'boxes': boxes,\n",
    "            'labels': labels,\n",
    "            'image_id': torch.tensor([index]),\n",
    "            'area': areas,\n",
    "            'iscrowd': iscrowd\n",
    "        }\n",
    "        \n",
    "        if self.transforms:\n",
    "            result_aug = self.transforms(image=image, bboxes=boxes, labels=labels)\n",
    "            image = result_aug['image'].float()\n",
    "            \n",
    "            target['boxes'] = torch.stack(tuple(map(torch.tensor, zip(*result_aug['bboxes'])))).permute(1, 0)\n",
    "\n",
    "        return image, target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_model(pre_trained=True):\n",
    "    \n",
    "    # Reference: https://stackoverflow.com/questions/58362892/resnet-18-as-backbone-in-faster-r-cnn\n",
    "    resnet_net = torchvision.models.resnet101(pretrained=True) \n",
    "    modules = list(resnet_net.children())[:-2]\n",
    "\n",
    "    backbone = nn.Sequential(*modules)\n",
    "    backbone.out_channels = 2048\n",
    "\n",
    "    # let's make the RPN generate 5 x 3 anchors per spatial\n",
    "    # location, with 5 different sizes and 3 different aspect\n",
    "    # ratios. We have a Tuple[Tuple[int]] because each feature\n",
    "    # map could potentially have different sizes and\n",
    "    # aspect ratios\n",
    "    anchor_generator = AnchorGenerator(sizes=((32, 64, 128, 256, 512),),\n",
    "                                       aspect_ratios=((0.5, 1.0, 2.0),))\n",
    "\n",
    "    # put the pieces together inside a FasterRCNN model\n",
    "    model = FasterRCNN(backbone,\n",
    "                       num_classes=N_CLASSES,\n",
    "                       rpn_anchor_generator=anchor_generator)\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture\n",
    "# get the model using our helper function\n",
    "model = get_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py:2854: UserWarning: The default behavior for interpolate/upsample with float scale_factor will change in 1.6.0 to align with other frameworks/libraries, and use scale_factor directly, instead of relying on the computed output size. If you wish to keep the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. \n",
      "  warnings.warn(\"The default behavior for interpolate/upsample with float scale_factor will change \"\n",
      "/opt/conda/conda-bld/pytorch_1587428398394/work/torch/csrc/utils/python_arg_parser.cpp:756: UserWarning: This overload of nonzero is deprecated:\n",
      "\tnonzero(Tensor input, *, Tensor out)\n",
      "Consider using one of the following signatures instead:\n",
      "\tnonzero(Tensor input, *, bool as_tuple)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: [0]  [   0/2699]  eta: 2:16:37  lr: 0.000010  loss: 1.5669 (1.5669)  loss_classifier: 0.6506 (0.6506)  loss_box_reg: 0.0399 (0.0399)  loss_objectness: 0.6982 (0.6982)  loss_rpn_box_reg: 0.1783 (0.1783)  time: 3.0374  data: 0.8453  max mem: 5070\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py:2854: UserWarning: The default behavior for interpolate/upsample with float scale_factor will change in 1.6.0 to align with other frameworks/libraries, and use scale_factor directly, instead of relying on the computed output size. If you wish to keep the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. \n",
      "  warnings.warn(\"The default behavior for interpolate/upsample with float scale_factor will change \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: [0]  [ 100/2699]  eta: 0:23:37  lr: 0.000509  loss: 1.1720 (1.3646)  loss_classifier: 0.3961 (0.4589)  loss_box_reg: 0.2365 (0.1824)  loss_objectness: 0.3588 (0.4983)  loss_rpn_box_reg: 0.1972 (0.2249)  time: 0.5093  data: 0.0100  max mem: 6482\n",
      "Epoch: [0]  [ 200/2699]  eta: 0:22:04  lr: 0.001009  loss: 1.1450 (1.2446)  loss_classifier: 0.3691 (0.4168)  loss_box_reg: 0.3125 (0.2282)  loss_objectness: 0.2594 (0.3985)  loss_rpn_box_reg: 0.1777 (0.2010)  time: 0.5102  data: 0.0109  max mem: 6482\n",
      "Epoch: [0]  [ 300/2699]  eta: 0:20:59  lr: 0.001508  loss: 1.1144 (1.2179)  loss_classifier: 0.3552 (0.4054)  loss_box_reg: 0.4211 (0.2741)  loss_objectness: 0.2150 (0.3472)  loss_rpn_box_reg: 0.1635 (0.1912)  time: 0.5150  data: 0.0102  max mem: 6482\n",
      "Epoch: [0]  [ 400/2699]  eta: 0:19:59  lr: 0.002008  loss: 1.0888 (1.1841)  loss_classifier: 0.3652 (0.3949)  loss_box_reg: 0.3790 (0.3003)  loss_objectness: 0.1755 (0.3087)  loss_rpn_box_reg: 0.1351 (0.1802)  time: 0.5211  data: 0.0107  max mem: 6482\n",
      "Epoch: [0]  [ 500/2699]  eta: 0:19:02  lr: 0.002507  loss: 1.0291 (1.1506)  loss_classifier: 0.3544 (0.3859)  loss_box_reg: 0.3882 (0.3158)  loss_objectness: 0.1520 (0.2778)  loss_rpn_box_reg: 0.1513 (0.1710)  time: 0.5106  data: 0.0103  max mem: 6482\n",
      "Epoch: [0]  [ 600/2699]  eta: 0:18:08  lr: 0.003007  loss: 0.9642 (1.1137)  loss_classifier: 0.3280 (0.3753)  loss_box_reg: 0.3637 (0.3234)  loss_objectness: 0.1221 (0.2525)  loss_rpn_box_reg: 0.1116 (0.1626)  time: 0.5097  data: 0.0103  max mem: 6482\n",
      "Epoch: [0]  [ 700/2699]  eta: 0:17:14  lr: 0.003506  loss: 0.8571 (1.0831)  loss_classifier: 0.2995 (0.3651)  loss_box_reg: 0.3441 (0.3261)  loss_objectness: 0.1090 (0.2334)  loss_rpn_box_reg: 0.1312 (0.1586)  time: 0.5089  data: 0.0102  max mem: 6482\n",
      "Epoch: [0]  [ 800/2699]  eta: 0:16:21  lr: 0.004006  loss: 0.7574 (1.0485)  loss_classifier: 0.2582 (0.3552)  loss_box_reg: 0.3108 (0.3251)  loss_objectness: 0.0830 (0.2171)  loss_rpn_box_reg: 0.1009 (0.1512)  time: 0.5079  data: 0.0097  max mem: 6482\n",
      "Epoch: [0]  [ 900/2699]  eta: 0:15:29  lr: 0.004505  loss: 0.7794 (1.0211)  loss_classifier: 0.2944 (0.3475)  loss_box_reg: 0.3127 (0.3232)  loss_objectness: 0.0964 (0.2042)  loss_rpn_box_reg: 0.1090 (0.1463)  time: 0.5110  data: 0.0101  max mem: 6482\n",
      "Epoch: [0]  [1000/2699]  eta: 0:14:36  lr: 0.005000  loss: 0.8076 (0.9947)  loss_classifier: 0.2897 (0.3400)  loss_box_reg: 0.3251 (0.3205)  loss_objectness: 0.0806 (0.1927)  loss_rpn_box_reg: 0.1066 (0.1414)  time: 0.5175  data: 0.0109  max mem: 6482\n",
      "Epoch: [0]  [1100/2699]  eta: 0:13:44  lr: 0.005000  loss: 0.7240 (0.9717)  loss_classifier: 0.2633 (0.3337)  loss_box_reg: 0.3009 (0.3184)  loss_objectness: 0.0742 (0.1827)  loss_rpn_box_reg: 0.0876 (0.1369)  time: 0.5175  data: 0.0107  max mem: 6482\n",
      "Epoch: [0]  [1200/2699]  eta: 0:12:52  lr: 0.005000  loss: 0.7498 (0.9529)  loss_classifier: 0.2669 (0.3286)  loss_box_reg: 0.2950 (0.3160)  loss_objectness: 0.0812 (0.1746)  loss_rpn_box_reg: 0.0913 (0.1336)  time: 0.5071  data: 0.0096  max mem: 6482\n",
      "Epoch: [0]  [1300/2699]  eta: 0:12:00  lr: 0.005000  loss: 0.6925 (0.9340)  loss_classifier: 0.2578 (0.3234)  loss_box_reg: 0.2809 (0.3131)  loss_objectness: 0.0642 (0.1671)  loss_rpn_box_reg: 0.0805 (0.1304)  time: 0.5092  data: 0.0102  max mem: 6482\n",
      "Epoch: [0]  [1400/2699]  eta: 0:11:08  lr: 0.005000  loss: 0.6735 (0.9180)  loss_classifier: 0.2582 (0.3193)  loss_box_reg: 0.2955 (0.3104)  loss_objectness: 0.0606 (0.1608)  loss_rpn_box_reg: 0.0859 (0.1274)  time: 0.5085  data: 0.0102  max mem: 6482\n",
      "Epoch: [0]  [1500/2699]  eta: 0:10:16  lr: 0.005000  loss: 0.6209 (0.9021)  loss_classifier: 0.2401 (0.3152)  loss_box_reg: 0.2464 (0.3072)  loss_objectness: 0.0534 (0.1549)  loss_rpn_box_reg: 0.0705 (0.1247)  time: 0.5076  data: 0.0097  max mem: 6482\n",
      "Epoch: [0]  [1600/2699]  eta: 0:09:25  lr: 0.005000  loss: 0.6590 (0.8880)  loss_classifier: 0.2399 (0.3116)  loss_box_reg: 0.2606 (0.3045)  loss_objectness: 0.0550 (0.1497)  loss_rpn_box_reg: 0.0756 (0.1221)  time: 0.5124  data: 0.0101  max mem: 6482\n",
      "Epoch: [0]  [1700/2699]  eta: 0:08:33  lr: 0.005000  loss: 0.7088 (0.8763)  loss_classifier: 0.2588 (0.3084)  loss_box_reg: 0.2782 (0.3021)  loss_objectness: 0.0671 (0.1453)  loss_rpn_box_reg: 0.0904 (0.1205)  time: 0.5160  data: 0.0103  max mem: 6482\n",
      "Epoch: [0]  [1800/2699]  eta: 0:07:41  lr: 0.005000  loss: 0.5578 (0.8622)  loss_classifier: 0.2038 (0.3044)  loss_box_reg: 0.2342 (0.2990)  loss_objectness: 0.0501 (0.1408)  loss_rpn_box_reg: 0.0554 (0.1180)  time: 0.5133  data: 0.0110  max mem: 6482\n",
      "Epoch: [0]  [1900/2699]  eta: 0:06:50  lr: 0.005000  loss: 0.6656 (0.8521)  loss_classifier: 0.2478 (0.3017)  loss_box_reg: 0.2648 (0.2971)  loss_objectness: 0.0574 (0.1369)  loss_rpn_box_reg: 0.0880 (0.1164)  time: 0.5061  data: 0.0099  max mem: 6482\n",
      "Epoch: [0]  [2000/2699]  eta: 0:05:58  lr: 0.005000  loss: 0.6154 (0.8421)  loss_classifier: 0.2218 (0.2989)  loss_box_reg: 0.2414 (0.2948)  loss_objectness: 0.0639 (0.1334)  loss_rpn_box_reg: 0.0718 (0.1149)  time: 0.5084  data: 0.0103  max mem: 6482\n",
      "Epoch: [0]  [2100/2699]  eta: 0:05:07  lr: 0.005000  loss: 0.6049 (0.8309)  loss_classifier: 0.2356 (0.2957)  loss_box_reg: 0.2416 (0.2922)  loss_objectness: 0.0619 (0.1300)  loss_rpn_box_reg: 0.0763 (0.1131)  time: 0.5075  data: 0.0099  max mem: 6482\n",
      "Epoch: [0]  [2200/2699]  eta: 0:04:16  lr: 0.005000  loss: 0.6006 (0.8217)  loss_classifier: 0.2279 (0.2933)  loss_box_reg: 0.2279 (0.2901)  loss_objectness: 0.0564 (0.1268)  loss_rpn_box_reg: 0.0635 (0.1115)  time: 0.5067  data: 0.0098  max mem: 6482\n",
      "Epoch: [0]  [2300/2699]  eta: 0:03:24  lr: 0.005000  loss: 0.6006 (0.8131)  loss_classifier: 0.2387 (0.2911)  loss_box_reg: 0.2403 (0.2880)  loss_objectness: 0.0540 (0.1239)  loss_rpn_box_reg: 0.0678 (0.1101)  time: 0.5091  data: 0.0097  max mem: 6482\n",
      "Epoch: [0]  [2400/2699]  eta: 0:02:33  lr: 0.005000  loss: 0.6165 (0.8045)  loss_classifier: 0.2445 (0.2888)  loss_box_reg: 0.2279 (0.2858)  loss_objectness: 0.0571 (0.1213)  loss_rpn_box_reg: 0.0818 (0.1088)  time: 0.5230  data: 0.0121  max mem: 6482\n",
      "Epoch: [0]  [2500/2699]  eta: 0:01:42  lr: 0.005000  loss: 0.6616 (0.7974)  loss_classifier: 0.2491 (0.2868)  loss_box_reg: 0.2531 (0.2840)  loss_objectness: 0.0657 (0.1189)  loss_rpn_box_reg: 0.0835 (0.1077)  time: 0.5063  data: 0.0102  max mem: 6482\n",
      "Epoch: [0]  [2600/2699]  eta: 0:00:50  lr: 0.005000  loss: 0.5033 (0.7897)  loss_classifier: 0.1998 (0.2845)  loss_box_reg: 0.1986 (0.2820)  loss_objectness: 0.0462 (0.1166)  loss_rpn_box_reg: 0.0637 (0.1066)  time: 0.5100  data: 0.0108  max mem: 6482\n",
      "Epoch: [0]  [2698/2699]  eta: 0:00:00  lr: 0.005000  loss: 0.5703 (0.7817)  loss_classifier: 0.2230 (0.2822)  loss_box_reg: 0.2238 (0.2799)  loss_objectness: 0.0535 (0.1143)  loss_rpn_box_reg: 0.0654 (0.1053)  time: 1.0371  data: 0.5533  max mem: 6482\n",
      "Epoch: [0] Total time: 0:23:14 (0.5167 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:13:20  model_time: 0.1958 (0.1958)  evaluator_time: 0.3505 (0.3505)  time: 1.1857  data: 0.6129  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:05:31  model_time: 0.1536 (0.1592)  evaluator_time: 0.3347 (0.3919)  time: 0.5105  data: 0.0101  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:05:25  model_time: 0.1601 (0.1593)  evaluator_time: 0.7772 (0.5040)  time: 0.9346  data: 0.0099  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:57  model_time: 0.1555 (0.1594)  evaluator_time: 0.3723 (0.4531)  time: 0.5676  data: 0.0100  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:47  model_time: 0.1582 (0.1595)  evaluator_time: 0.5113 (0.4282)  time: 0.6964  data: 0.0103  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:44  model_time: 0.1583 (0.1593)  evaluator_time: 0.1880 (0.4170)  time: 0.6428  data: 0.0104  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:46  model_time: 0.1622 (0.1593)  evaluator_time: 0.1164 (0.4442)  time: 0.3961  data: 0.0103  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1604 (0.1593)  evaluator_time: 0.1042 (0.4270)  time: 0.2876  data: 0.0096  max mem: 6482\n",
      "Test: Total time: 0:06:49 (0.6064 s / it)\n",
      "Averaged stats: model_time: 0.1604 (0.1593)  evaluator_time: 0.1042 (0.4270)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.84s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.378\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.850\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.260\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.012\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.367\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.446\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.014\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.131\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.462\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.064\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.444\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.550\n",
      "Epoch: [1]  [   0/2699]  eta: 1:16:08  lr: 0.005000  loss: 0.4874 (0.4874)  loss_classifier: 0.1948 (0.1948)  loss_box_reg: 0.2119 (0.2119)  loss_objectness: 0.0325 (0.0325)  loss_rpn_box_reg: 0.0482 (0.0482)  time: 1.6925  data: 1.0859  max mem: 6482\n",
      "Epoch: [1]  [ 100/2699]  eta: 0:22:38  lr: 0.005000  loss: 0.5970 (0.5657)  loss_classifier: 0.2250 (0.2168)  loss_box_reg: 0.2156 (0.2208)  loss_objectness: 0.0577 (0.0519)  loss_rpn_box_reg: 0.0782 (0.0763)  time: 0.5103  data: 0.0106  max mem: 6482\n",
      "Epoch: [1]  [ 200/2699]  eta: 0:21:30  lr: 0.005000  loss: 0.5860 (0.5594)  loss_classifier: 0.2204 (0.2161)  loss_box_reg: 0.2349 (0.2181)  loss_objectness: 0.0510 (0.0509)  loss_rpn_box_reg: 0.0782 (0.0744)  time: 0.5205  data: 0.0124  max mem: 6482\n",
      "Epoch: [1]  [ 300/2699]  eta: 0:20:32  lr: 0.005000  loss: 0.5950 (0.5622)  loss_classifier: 0.2277 (0.2172)  loss_box_reg: 0.2221 (0.2187)  loss_objectness: 0.0415 (0.0515)  loss_rpn_box_reg: 0.0820 (0.0748)  time: 0.5075  data: 0.0100  max mem: 6482\n",
      "Epoch: [1]  [ 400/2699]  eta: 0:19:39  lr: 0.005000  loss: 0.6061 (0.5645)  loss_classifier: 0.2343 (0.2185)  loss_box_reg: 0.2197 (0.2200)  loss_objectness: 0.0467 (0.0515)  loss_rpn_box_reg: 0.0848 (0.0745)  time: 0.5113  data: 0.0109  max mem: 6482\n",
      "Epoch: [1]  [ 500/2699]  eta: 0:18:47  lr: 0.005000  loss: 0.6067 (0.5661)  loss_classifier: 0.2380 (0.2194)  loss_box_reg: 0.2310 (0.2206)  loss_objectness: 0.0515 (0.0517)  loss_rpn_box_reg: 0.0729 (0.0745)  time: 0.5091  data: 0.0102  max mem: 6482\n",
      "Epoch: [1]  [ 600/2699]  eta: 0:17:55  lr: 0.005000  loss: 0.5407 (0.5646)  loss_classifier: 0.1987 (0.2193)  loss_box_reg: 0.2078 (0.2197)  loss_objectness: 0.0423 (0.0518)  loss_rpn_box_reg: 0.0610 (0.0738)  time: 0.5068  data: 0.0102  max mem: 6482\n",
      "Epoch: [1]  [ 700/2699]  eta: 0:17:04  lr: 0.005000  loss: 0.5529 (0.5634)  loss_classifier: 0.2089 (0.2188)  loss_box_reg: 0.2278 (0.2193)  loss_objectness: 0.0456 (0.0516)  loss_rpn_box_reg: 0.0698 (0.0737)  time: 0.5110  data: 0.0113  max mem: 6482\n",
      "Epoch: [1]  [ 800/2699]  eta: 0:16:13  lr: 0.005000  loss: 0.5086 (0.5591)  loss_classifier: 0.1944 (0.2173)  loss_box_reg: 0.2219 (0.2180)  loss_objectness: 0.0373 (0.0509)  loss_rpn_box_reg: 0.0554 (0.0728)  time: 0.5113  data: 0.0107  max mem: 6482\n",
      "Epoch: [1]  [ 900/2699]  eta: 0:15:21  lr: 0.005000  loss: 0.5898 (0.5610)  loss_classifier: 0.2329 (0.2184)  loss_box_reg: 0.2191 (0.2185)  loss_objectness: 0.0461 (0.0511)  loss_rpn_box_reg: 0.0699 (0.0729)  time: 0.5179  data: 0.0114  max mem: 6482\n",
      "Epoch: [1]  [1000/2699]  eta: 0:14:29  lr: 0.005000  loss: 0.5665 (0.5612)  loss_classifier: 0.2202 (0.2184)  loss_box_reg: 0.2183 (0.2185)  loss_objectness: 0.0470 (0.0511)  loss_rpn_box_reg: 0.0643 (0.0731)  time: 0.5072  data: 0.0104  max mem: 6482\n",
      "Epoch: [1]  [1100/2699]  eta: 0:13:38  lr: 0.005000  loss: 0.5259 (0.5590)  loss_classifier: 0.2120 (0.2179)  loss_box_reg: 0.2087 (0.2176)  loss_objectness: 0.0435 (0.0509)  loss_rpn_box_reg: 0.0624 (0.0726)  time: 0.5081  data: 0.0101  max mem: 6482\n",
      "Epoch: [1]  [1200/2699]  eta: 0:12:46  lr: 0.005000  loss: 0.5031 (0.5590)  loss_classifier: 0.1909 (0.2180)  loss_box_reg: 0.2009 (0.2176)  loss_objectness: 0.0404 (0.0510)  loss_rpn_box_reg: 0.0697 (0.0724)  time: 0.5086  data: 0.0106  max mem: 6482\n",
      "Epoch: [1]  [1300/2699]  eta: 0:11:55  lr: 0.005000  loss: 0.5651 (0.5586)  loss_classifier: 0.2248 (0.2178)  loss_box_reg: 0.2346 (0.2175)  loss_objectness: 0.0437 (0.0509)  loss_rpn_box_reg: 0.0653 (0.0724)  time: 0.5105  data: 0.0108  max mem: 6482\n",
      "Epoch: [1]  [1400/2699]  eta: 0:11:04  lr: 0.005000  loss: 0.5421 (0.5571)  loss_classifier: 0.2193 (0.2173)  loss_box_reg: 0.2079 (0.2170)  loss_objectness: 0.0406 (0.0507)  loss_rpn_box_reg: 0.0701 (0.0721)  time: 0.5105  data: 0.0108  max mem: 6482\n",
      "Epoch: [1]  [1500/2699]  eta: 0:10:13  lr: 0.005000  loss: 0.5708 (0.5567)  loss_classifier: 0.2169 (0.2170)  loss_box_reg: 0.2225 (0.2168)  loss_objectness: 0.0498 (0.0508)  loss_rpn_box_reg: 0.0710 (0.0721)  time: 0.5160  data: 0.0109  max mem: 6482\n",
      "Epoch: [1]  [1600/2699]  eta: 0:09:22  lr: 0.005000  loss: 0.5163 (0.5546)  loss_classifier: 0.2110 (0.2164)  loss_box_reg: 0.2126 (0.2159)  loss_objectness: 0.0459 (0.0506)  loss_rpn_box_reg: 0.0614 (0.0717)  time: 0.5230  data: 0.0108  max mem: 6482\n",
      "Epoch: [1]  [1700/2699]  eta: 0:08:31  lr: 0.005000  loss: 0.5172 (0.5540)  loss_classifier: 0.2062 (0.2161)  loss_box_reg: 0.1926 (0.2155)  loss_objectness: 0.0453 (0.0507)  loss_rpn_box_reg: 0.0660 (0.0718)  time: 0.5120  data: 0.0103  max mem: 6482\n",
      "Epoch: [1]  [1800/2699]  eta: 0:07:40  lr: 0.005000  loss: 0.5101 (0.5528)  loss_classifier: 0.1965 (0.2156)  loss_box_reg: 0.1932 (0.2149)  loss_objectness: 0.0441 (0.0505)  loss_rpn_box_reg: 0.0628 (0.0718)  time: 0.5076  data: 0.0102  max mem: 6482\n",
      "Epoch: [1]  [1900/2699]  eta: 0:06:49  lr: 0.005000  loss: 0.5393 (0.5520)  loss_classifier: 0.2060 (0.2154)  loss_box_reg: 0.1995 (0.2145)  loss_objectness: 0.0474 (0.0505)  loss_rpn_box_reg: 0.0717 (0.0717)  time: 0.5110  data: 0.0113  max mem: 6482\n",
      "Epoch: [1]  [2000/2699]  eta: 0:05:57  lr: 0.005000  loss: 0.5360 (0.5504)  loss_classifier: 0.2131 (0.2149)  loss_box_reg: 0.2023 (0.2139)  loss_objectness: 0.0493 (0.0503)  loss_rpn_box_reg: 0.0608 (0.0714)  time: 0.5090  data: 0.0107  max mem: 6482\n",
      "Epoch: [1]  [2100/2699]  eta: 0:05:06  lr: 0.005000  loss: 0.5716 (0.5487)  loss_classifier: 0.2199 (0.2142)  loss_box_reg: 0.2038 (0.2132)  loss_objectness: 0.0438 (0.0500)  loss_rpn_box_reg: 0.0833 (0.0712)  time: 0.5083  data: 0.0104  max mem: 6482\n",
      "Epoch: [1]  [2200/2699]  eta: 0:04:15  lr: 0.005000  loss: 0.5332 (0.5494)  loss_classifier: 0.2190 (0.2145)  loss_box_reg: 0.1936 (0.2133)  loss_objectness: 0.0510 (0.0501)  loss_rpn_box_reg: 0.0620 (0.0714)  time: 0.5178  data: 0.0122  max mem: 6482\n",
      "Epoch: [1]  [2300/2699]  eta: 0:03:24  lr: 0.005000  loss: 0.5430 (0.5491)  loss_classifier: 0.2029 (0.2145)  loss_box_reg: 0.2152 (0.2131)  loss_objectness: 0.0482 (0.0500)  loss_rpn_box_reg: 0.0695 (0.0715)  time: 0.5173  data: 0.0127  max mem: 6482\n",
      "Epoch: [1]  [2400/2699]  eta: 0:02:33  lr: 0.005000  loss: 0.4734 (0.5487)  loss_classifier: 0.1919 (0.2143)  loss_box_reg: 0.1860 (0.2129)  loss_objectness: 0.0345 (0.0501)  loss_rpn_box_reg: 0.0655 (0.0715)  time: 0.5102  data: 0.0105  max mem: 6482\n",
      "Epoch: [1]  [2500/2699]  eta: 0:01:41  lr: 0.005000  loss: 0.4850 (0.5477)  loss_classifier: 0.1945 (0.2139)  loss_box_reg: 0.1740 (0.2125)  loss_objectness: 0.0486 (0.0500)  loss_rpn_box_reg: 0.0567 (0.0713)  time: 0.5073  data: 0.0100  max mem: 6482\n",
      "Epoch: [1]  [2600/2699]  eta: 0:00:50  lr: 0.005000  loss: 0.4742 (0.5462)  loss_classifier: 0.1882 (0.2134)  loss_box_reg: 0.2009 (0.2120)  loss_objectness: 0.0359 (0.0498)  loss_rpn_box_reg: 0.0515 (0.0711)  time: 0.5086  data: 0.0105  max mem: 6482\n",
      "Epoch: [1]  [2698/2699]  eta: 0:00:00  lr: 0.005000  loss: 0.5694 (0.5458)  loss_classifier: 0.2236 (0.2132)  loss_box_reg: 0.2205 (0.2119)  loss_objectness: 0.0476 (0.0497)  loss_rpn_box_reg: 0.0663 (0.0711)  time: 0.4954  data: 0.0106  max mem: 6482\n",
      "Epoch: [1] Total time: 0:23:01 (0.5120 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:16:18  model_time: 0.2463 (0.2463)  evaluator_time: 0.5767 (0.5767)  time: 1.4497  data: 0.6030  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:05:13  model_time: 0.1541 (0.1575)  evaluator_time: 0.2984 (0.3622)  time: 0.4760  data: 0.0104  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:05:15  model_time: 0.1558 (0.1575)  evaluator_time: 0.7861 (0.4822)  time: 0.9415  data: 0.0135  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:49  model_time: 0.1570 (0.1581)  evaluator_time: 0.3538 (0.4310)  time: 0.5641  data: 0.0105  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:40  model_time: 0.1552 (0.1579)  evaluator_time: 0.4883 (0.4055)  time: 0.6683  data: 0.0107  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:40  model_time: 0.1552 (0.1578)  evaluator_time: 0.1693 (0.3932)  time: 0.6227  data: 0.0121  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:44  model_time: 0.1606 (0.1576)  evaluator_time: 0.1134 (0.4206)  time: 0.3922  data: 0.0106  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1591 (0.1577)  evaluator_time: 0.1022 (0.4040)  time: 0.2860  data: 0.0098  max mem: 6482\n",
      "Test: Total time: 0:06:33 (0.5825 s / it)\n",
      "Averaged stats: model_time: 0.1591 (0.1577)  evaluator_time: 0.1022 (0.4040)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.73s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.404\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.881\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.295\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.025\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.397\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.441\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.014\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.132\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.482\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.113\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.482\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.499\n",
      "Epoch: [2]  [   0/2699]  eta: 1:12:42  lr: 0.005000  loss: 0.3882 (0.3882)  loss_classifier: 0.1386 (0.1386)  loss_box_reg: 0.1954 (0.1954)  loss_objectness: 0.0155 (0.0155)  loss_rpn_box_reg: 0.0388 (0.0388)  time: 1.6164  data: 0.9382  max mem: 6482\n",
      "Epoch: [2]  [ 100/2699]  eta: 0:22:40  lr: 0.005000  loss: 0.4646 (0.4919)  loss_classifier: 0.1966 (0.1944)  loss_box_reg: 0.2021 (0.1935)  loss_objectness: 0.0320 (0.0401)  loss_rpn_box_reg: 0.0513 (0.0638)  time: 0.5087  data: 0.0102  max mem: 6482\n",
      "Epoch: [2]  [ 200/2699]  eta: 0:21:34  lr: 0.005000  loss: 0.5157 (0.4925)  loss_classifier: 0.2148 (0.1947)  loss_box_reg: 0.1834 (0.1925)  loss_objectness: 0.0335 (0.0409)  loss_rpn_box_reg: 0.0656 (0.0644)  time: 0.5112  data: 0.0105  max mem: 6482\n",
      "Epoch: [2]  [ 300/2699]  eta: 0:20:37  lr: 0.005000  loss: 0.4574 (0.5024)  loss_classifier: 0.1928 (0.1978)  loss_box_reg: 0.1822 (0.1951)  loss_objectness: 0.0354 (0.0431)  loss_rpn_box_reg: 0.0568 (0.0664)  time: 0.5261  data: 0.0117  max mem: 6482\n",
      "Epoch: [2]  [ 400/2699]  eta: 0:19:41  lr: 0.005000  loss: 0.5253 (0.4959)  loss_classifier: 0.2031 (0.1961)  loss_box_reg: 0.1949 (0.1929)  loss_objectness: 0.0378 (0.0421)  loss_rpn_box_reg: 0.0615 (0.0648)  time: 0.5066  data: 0.0100  max mem: 6482\n",
      "Epoch: [2]  [ 500/2699]  eta: 0:18:48  lr: 0.005000  loss: 0.5295 (0.4954)  loss_classifier: 0.2089 (0.1963)  loss_box_reg: 0.2138 (0.1927)  loss_objectness: 0.0408 (0.0415)  loss_rpn_box_reg: 0.0714 (0.0649)  time: 0.5067  data: 0.0102  max mem: 6482\n",
      "Epoch: [2]  [ 600/2699]  eta: 0:17:55  lr: 0.005000  loss: 0.4915 (0.4948)  loss_classifier: 0.1925 (0.1960)  loss_box_reg: 0.2049 (0.1928)  loss_objectness: 0.0386 (0.0413)  loss_rpn_box_reg: 0.0567 (0.0647)  time: 0.5074  data: 0.0101  max mem: 6482\n",
      "Epoch: [2]  [ 700/2699]  eta: 0:17:04  lr: 0.005000  loss: 0.4627 (0.4945)  loss_classifier: 0.1782 (0.1956)  loss_box_reg: 0.1782 (0.1927)  loss_objectness: 0.0391 (0.0417)  loss_rpn_box_reg: 0.0634 (0.0645)  time: 0.5101  data: 0.0106  max mem: 6482\n",
      "Epoch: [2]  [ 800/2699]  eta: 0:16:12  lr: 0.005000  loss: 0.4646 (0.4924)  loss_classifier: 0.1783 (0.1944)  loss_box_reg: 0.1892 (0.1923)  loss_objectness: 0.0364 (0.0413)  loss_rpn_box_reg: 0.0622 (0.0645)  time: 0.5090  data: 0.0104  max mem: 6482\n",
      "Epoch: [2]  [ 900/2699]  eta: 0:15:21  lr: 0.005000  loss: 0.5028 (0.4938)  loss_classifier: 0.1946 (0.1949)  loss_box_reg: 0.2012 (0.1925)  loss_objectness: 0.0377 (0.0414)  loss_rpn_box_reg: 0.0646 (0.0650)  time: 0.5164  data: 0.0121  max mem: 6482\n",
      "Epoch: [2]  [1000/2699]  eta: 0:14:29  lr: 0.005000  loss: 0.4793 (0.4921)  loss_classifier: 0.1929 (0.1944)  loss_box_reg: 0.1785 (0.1919)  loss_objectness: 0.0393 (0.0411)  loss_rpn_box_reg: 0.0559 (0.0648)  time: 0.5183  data: 0.0120  max mem: 6482\n",
      "Epoch: [2]  [1100/2699]  eta: 0:13:38  lr: 0.005000  loss: 0.4690 (0.4913)  loss_classifier: 0.1878 (0.1941)  loss_box_reg: 0.1806 (0.1917)  loss_objectness: 0.0329 (0.0409)  loss_rpn_box_reg: 0.0492 (0.0646)  time: 0.5077  data: 0.0104  max mem: 6482\n",
      "Epoch: [2]  [1200/2699]  eta: 0:12:46  lr: 0.005000  loss: 0.4525 (0.4902)  loss_classifier: 0.1768 (0.1938)  loss_box_reg: 0.1707 (0.1911)  loss_objectness: 0.0319 (0.0409)  loss_rpn_box_reg: 0.0567 (0.0643)  time: 0.5064  data: 0.0106  max mem: 6482\n",
      "Epoch: [2]  [1300/2699]  eta: 0:11:55  lr: 0.005000  loss: 0.5286 (0.4895)  loss_classifier: 0.2005 (0.1936)  loss_box_reg: 0.1973 (0.1908)  loss_objectness: 0.0414 (0.0409)  loss_rpn_box_reg: 0.0695 (0.0642)  time: 0.5141  data: 0.0115  max mem: 6482\n",
      "Epoch: [2]  [1400/2699]  eta: 0:11:04  lr: 0.005000  loss: 0.4497 (0.4886)  loss_classifier: 0.1842 (0.1931)  loss_box_reg: 0.1872 (0.1903)  loss_objectness: 0.0361 (0.0409)  loss_rpn_box_reg: 0.0615 (0.0642)  time: 0.5067  data: 0.0104  max mem: 6482\n",
      "Epoch: [2]  [1500/2699]  eta: 0:10:13  lr: 0.005000  loss: 0.4868 (0.4887)  loss_classifier: 0.1886 (0.1932)  loss_box_reg: 0.1831 (0.1905)  loss_objectness: 0.0406 (0.0409)  loss_rpn_box_reg: 0.0660 (0.0641)  time: 0.5111  data: 0.0115  max mem: 6482\n",
      "Epoch: [2]  [1600/2699]  eta: 0:09:22  lr: 0.005000  loss: 0.4936 (0.4894)  loss_classifier: 0.1968 (0.1934)  loss_box_reg: 0.1941 (0.1908)  loss_objectness: 0.0418 (0.0409)  loss_rpn_box_reg: 0.0676 (0.0642)  time: 0.5148  data: 0.0116  max mem: 6482\n",
      "Epoch: [2]  [1700/2699]  eta: 0:08:30  lr: 0.005000  loss: 0.4506 (0.4905)  loss_classifier: 0.1827 (0.1938)  loss_box_reg: 0.1815 (0.1911)  loss_objectness: 0.0364 (0.0411)  loss_rpn_box_reg: 0.0625 (0.0645)  time: 0.5161  data: 0.0106  max mem: 6482\n",
      "Epoch: [2]  [1800/2699]  eta: 0:07:39  lr: 0.005000  loss: 0.4896 (0.4895)  loss_classifier: 0.1992 (0.1936)  loss_box_reg: 0.1831 (0.1906)  loss_objectness: 0.0419 (0.0409)  loss_rpn_box_reg: 0.0593 (0.0644)  time: 0.5071  data: 0.0104  max mem: 6482\n",
      "Epoch: [2]  [1900/2699]  eta: 0:06:48  lr: 0.005000  loss: 0.5137 (0.4890)  loss_classifier: 0.2002 (0.1933)  loss_box_reg: 0.1975 (0.1904)  loss_objectness: 0.0381 (0.0409)  loss_rpn_box_reg: 0.0672 (0.0644)  time: 0.5107  data: 0.0113  max mem: 6482\n",
      "Epoch: [2]  [2000/2699]  eta: 0:05:57  lr: 0.005000  loss: 0.4697 (0.4889)  loss_classifier: 0.1745 (0.1932)  loss_box_reg: 0.1842 (0.1904)  loss_objectness: 0.0337 (0.0409)  loss_rpn_box_reg: 0.0614 (0.0644)  time: 0.5098  data: 0.0106  max mem: 6482\n",
      "Epoch: [2]  [2100/2699]  eta: 0:05:06  lr: 0.005000  loss: 0.4760 (0.4882)  loss_classifier: 0.1981 (0.1930)  loss_box_reg: 0.1943 (0.1901)  loss_objectness: 0.0340 (0.0408)  loss_rpn_box_reg: 0.0561 (0.0642)  time: 0.5082  data: 0.0105  max mem: 6482\n",
      "Epoch: [2]  [2200/2699]  eta: 0:04:15  lr: 0.005000  loss: 0.4725 (0.4891)  loss_classifier: 0.1721 (0.1934)  loss_box_reg: 0.1766 (0.1902)  loss_objectness: 0.0375 (0.0410)  loss_rpn_box_reg: 0.0670 (0.0645)  time: 0.5114  data: 0.0108  max mem: 6482\n",
      "Epoch: [2]  [2300/2699]  eta: 0:03:24  lr: 0.005000  loss: 0.4424 (0.4888)  loss_classifier: 0.1756 (0.1932)  loss_box_reg: 0.1838 (0.1901)  loss_objectness: 0.0400 (0.0410)  loss_rpn_box_reg: 0.0620 (0.0646)  time: 0.5204  data: 0.0115  max mem: 6482\n",
      "Epoch: [2]  [2400/2699]  eta: 0:02:32  lr: 0.005000  loss: 0.4928 (0.4890)  loss_classifier: 0.1903 (0.1932)  loss_box_reg: 0.1792 (0.1903)  loss_objectness: 0.0395 (0.0409)  loss_rpn_box_reg: 0.0612 (0.0646)  time: 0.5139  data: 0.0105  max mem: 6482\n",
      "Epoch: [2]  [2500/2699]  eta: 0:01:41  lr: 0.005000  loss: 0.4768 (0.4882)  loss_classifier: 0.1826 (0.1929)  loss_box_reg: 0.1982 (0.1900)  loss_objectness: 0.0378 (0.0408)  loss_rpn_box_reg: 0.0602 (0.0645)  time: 0.5103  data: 0.0109  max mem: 6482\n",
      "Epoch: [2]  [2600/2699]  eta: 0:00:50  lr: 0.005000  loss: 0.4764 (0.4884)  loss_classifier: 0.1820 (0.1929)  loss_box_reg: 0.1936 (0.1901)  loss_objectness: 0.0296 (0.0407)  loss_rpn_box_reg: 0.0630 (0.0647)  time: 0.5078  data: 0.0103  max mem: 6482\n",
      "Epoch: [2]  [2698/2699]  eta: 0:00:00  lr: 0.005000  loss: 0.5204 (0.4885)  loss_classifier: 0.2114 (0.1931)  loss_box_reg: 0.2015 (0.1900)  loss_objectness: 0.0388 (0.0408)  loss_rpn_box_reg: 0.0629 (0.0647)  time: 0.4957  data: 0.0103  max mem: 6482\n",
      "Epoch: [2] Total time: 0:23:00 (0.5113 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:13:18  model_time: 0.1993 (0.1993)  evaluator_time: 0.3394 (0.3394)  time: 1.1836  data: 0.6314  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:05:17  model_time: 0.1500 (0.1519)  evaluator_time: 0.3365 (0.3762)  time: 0.5088  data: 0.0101  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:05:15  model_time: 0.1504 (0.1515)  evaluator_time: 0.7996 (0.4893)  time: 0.9403  data: 0.0110  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:48  model_time: 0.1529 (0.1525)  evaluator_time: 0.3500 (0.4347)  time: 0.5451  data: 0.0113  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:40  model_time: 0.1491 (0.1525)  evaluator_time: 0.5101 (0.4087)  time: 0.6725  data: 0.0109  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:40  model_time: 0.1521 (0.1523)  evaluator_time: 0.1753 (0.4005)  time: 0.6230  data: 0.0111  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:45  model_time: 0.1571 (0.1524)  evaluator_time: 0.1210 (0.4273)  time: 0.4086  data: 0.0110  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1529 (0.1524)  evaluator_time: 0.1276 (0.4085)  time: 0.3063  data: 0.0103  max mem: 6482\n",
      "Test: Total time: 0:06:32 (0.5814 s / it)\n",
      "Averaged stats: model_time: 0.1529 (0.1524)  evaluator_time: 0.1276 (0.4085)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.74s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.436\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.894\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.365\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.050\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.425\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.494\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.015\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.140\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.508\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.126\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.500\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.560\n",
      "Epoch: [3]  [   0/2699]  eta: 1:13:40  lr: 0.000500  loss: 0.4578 (0.4578)  loss_classifier: 0.1792 (0.1792)  loss_box_reg: 0.1871 (0.1871)  loss_objectness: 0.0163 (0.0163)  loss_rpn_box_reg: 0.0751 (0.0751)  time: 1.6378  data: 1.0751  max mem: 6482\n",
      "Epoch: [3]  [ 100/2699]  eta: 0:22:42  lr: 0.000500  loss: 0.4382 (0.4306)  loss_classifier: 0.1705 (0.1718)  loss_box_reg: 0.1693 (0.1692)  loss_objectness: 0.0359 (0.0343)  loss_rpn_box_reg: 0.0657 (0.0554)  time: 0.5088  data: 0.0106  max mem: 6482\n",
      "Epoch: [3]  [ 200/2699]  eta: 0:21:36  lr: 0.000500  loss: 0.3945 (0.4262)  loss_classifier: 0.1685 (0.1717)  loss_box_reg: 0.1616 (0.1662)  loss_objectness: 0.0249 (0.0333)  loss_rpn_box_reg: 0.0507 (0.0549)  time: 0.5087  data: 0.0107  max mem: 6482\n",
      "Epoch: [3]  [ 300/2699]  eta: 0:20:39  lr: 0.000500  loss: 0.4254 (0.4284)  loss_classifier: 0.1713 (0.1734)  loss_box_reg: 0.1597 (0.1659)  loss_objectness: 0.0294 (0.0333)  loss_rpn_box_reg: 0.0587 (0.0558)  time: 0.5177  data: 0.0119  max mem: 6482\n",
      "Epoch: [3]  [ 400/2699]  eta: 0:19:44  lr: 0.000500  loss: 0.4041 (0.4268)  loss_classifier: 0.1621 (0.1728)  loss_box_reg: 0.1656 (0.1656)  loss_objectness: 0.0312 (0.0330)  loss_rpn_box_reg: 0.0472 (0.0555)  time: 0.5189  data: 0.0132  max mem: 6482\n",
      "Epoch: [3]  [ 500/2699]  eta: 0:18:50  lr: 0.000500  loss: 0.4195 (0.4251)  loss_classifier: 0.1787 (0.1722)  loss_box_reg: 0.1736 (0.1651)  loss_objectness: 0.0281 (0.0328)  loss_rpn_box_reg: 0.0574 (0.0551)  time: 0.5085  data: 0.0108  max mem: 6482\n",
      "Epoch: [3]  [ 600/2699]  eta: 0:17:58  lr: 0.000500  loss: 0.3921 (0.4255)  loss_classifier: 0.1591 (0.1722)  loss_box_reg: 0.1530 (0.1648)  loss_objectness: 0.0342 (0.0333)  loss_rpn_box_reg: 0.0535 (0.0552)  time: 0.5117  data: 0.0110  max mem: 6482\n",
      "Epoch: [3]  [ 700/2699]  eta: 0:17:07  lr: 0.000500  loss: 0.4146 (0.4254)  loss_classifier: 0.1672 (0.1720)  loss_box_reg: 0.1616 (0.1646)  loss_objectness: 0.0293 (0.0332)  loss_rpn_box_reg: 0.0569 (0.0556)  time: 0.5098  data: 0.0108  max mem: 6482\n",
      "Epoch: [3]  [ 800/2699]  eta: 0:16:15  lr: 0.000500  loss: 0.4512 (0.4249)  loss_classifier: 0.1828 (0.1718)  loss_box_reg: 0.1655 (0.1646)  loss_objectness: 0.0298 (0.0330)  loss_rpn_box_reg: 0.0534 (0.0556)  time: 0.5094  data: 0.0107  max mem: 6482\n",
      "Epoch: [3]  [ 900/2699]  eta: 0:15:24  lr: 0.000500  loss: 0.3753 (0.4221)  loss_classifier: 0.1487 (0.1708)  loss_box_reg: 0.1541 (0.1638)  loss_objectness: 0.0217 (0.0324)  loss_rpn_box_reg: 0.0483 (0.0551)  time: 0.5086  data: 0.0105  max mem: 6482\n",
      "Epoch: [3]  [1000/2699]  eta: 0:14:32  lr: 0.000500  loss: 0.4008 (0.4202)  loss_classifier: 0.1619 (0.1701)  loss_box_reg: 0.1636 (0.1634)  loss_objectness: 0.0208 (0.0320)  loss_rpn_box_reg: 0.0440 (0.0548)  time: 0.5154  data: 0.0117  max mem: 6482\n",
      "Epoch: [3]  [1100/2699]  eta: 0:13:40  lr: 0.000500  loss: 0.4158 (0.4201)  loss_classifier: 0.1787 (0.1701)  loss_box_reg: 0.1591 (0.1631)  loss_objectness: 0.0316 (0.0320)  loss_rpn_box_reg: 0.0517 (0.0548)  time: 0.5233  data: 0.0110  max mem: 6482\n",
      "Epoch: [3]  [1200/2699]  eta: 0:12:49  lr: 0.000500  loss: 0.3687 (0.4199)  loss_classifier: 0.1495 (0.1702)  loss_box_reg: 0.1507 (0.1629)  loss_objectness: 0.0249 (0.0321)  loss_rpn_box_reg: 0.0433 (0.0547)  time: 0.5076  data: 0.0105  max mem: 6482\n",
      "Epoch: [3]  [1300/2699]  eta: 0:11:57  lr: 0.000500  loss: 0.3608 (0.4200)  loss_classifier: 0.1637 (0.1703)  loss_box_reg: 0.1412 (0.1628)  loss_objectness: 0.0234 (0.0320)  loss_rpn_box_reg: 0.0486 (0.0549)  time: 0.5130  data: 0.0110  max mem: 6482\n",
      "Epoch: [3]  [1400/2699]  eta: 0:11:06  lr: 0.000500  loss: 0.3786 (0.4180)  loss_classifier: 0.1477 (0.1693)  loss_box_reg: 0.1471 (0.1622)  loss_objectness: 0.0254 (0.0319)  loss_rpn_box_reg: 0.0441 (0.0546)  time: 0.5075  data: 0.0101  max mem: 6482\n",
      "Epoch: [3]  [1500/2699]  eta: 0:10:15  lr: 0.000500  loss: 0.3994 (0.4181)  loss_classifier: 0.1936 (0.1693)  loss_box_reg: 0.1649 (0.1622)  loss_objectness: 0.0295 (0.0320)  loss_rpn_box_reg: 0.0439 (0.0547)  time: 0.5091  data: 0.0102  max mem: 6482\n",
      "Epoch: [3]  [1600/2699]  eta: 0:09:23  lr: 0.000500  loss: 0.3877 (0.4179)  loss_classifier: 0.1529 (0.1691)  loss_box_reg: 0.1516 (0.1621)  loss_objectness: 0.0220 (0.0320)  loss_rpn_box_reg: 0.0574 (0.0547)  time: 0.5126  data: 0.0110  max mem: 6482\n",
      "Epoch: [3]  [1700/2699]  eta: 0:08:32  lr: 0.000500  loss: 0.3460 (0.4165)  loss_classifier: 0.1494 (0.1686)  loss_box_reg: 0.1360 (0.1617)  loss_objectness: 0.0238 (0.0318)  loss_rpn_box_reg: 0.0485 (0.0545)  time: 0.5175  data: 0.0131  max mem: 6482\n",
      "Epoch: [3]  [1800/2699]  eta: 0:07:40  lr: 0.000500  loss: 0.3713 (0.4160)  loss_classifier: 0.1603 (0.1685)  loss_box_reg: 0.1580 (0.1614)  loss_objectness: 0.0291 (0.0316)  loss_rpn_box_reg: 0.0519 (0.0545)  time: 0.5147  data: 0.0111  max mem: 6482\n",
      "Epoch: [3]  [1900/2699]  eta: 0:06:49  lr: 0.000500  loss: 0.3949 (0.4157)  loss_classifier: 0.1611 (0.1684)  loss_box_reg: 0.1571 (0.1613)  loss_objectness: 0.0244 (0.0316)  loss_rpn_box_reg: 0.0425 (0.0545)  time: 0.5112  data: 0.0114  max mem: 6482\n",
      "Epoch: [3]  [2000/2699]  eta: 0:05:58  lr: 0.000500  loss: 0.4371 (0.4154)  loss_classifier: 0.1853 (0.1683)  loss_box_reg: 0.1752 (0.1611)  loss_objectness: 0.0355 (0.0315)  loss_rpn_box_reg: 0.0542 (0.0545)  time: 0.5081  data: 0.0105  max mem: 6482\n",
      "Epoch: [3]  [2100/2699]  eta: 0:05:07  lr: 0.000500  loss: 0.3939 (0.4147)  loss_classifier: 0.1622 (0.1681)  loss_box_reg: 0.1417 (0.1607)  loss_objectness: 0.0242 (0.0314)  loss_rpn_box_reg: 0.0481 (0.0544)  time: 0.5100  data: 0.0103  max mem: 6482\n",
      "Epoch: [3]  [2200/2699]  eta: 0:04:15  lr: 0.000500  loss: 0.3959 (0.4135)  loss_classifier: 0.1620 (0.1677)  loss_box_reg: 0.1615 (0.1604)  loss_objectness: 0.0238 (0.0312)  loss_rpn_box_reg: 0.0513 (0.0542)  time: 0.5090  data: 0.0105  max mem: 6482\n",
      "Epoch: [3]  [2300/2699]  eta: 0:03:24  lr: 0.000500  loss: 0.4364 (0.4130)  loss_classifier: 0.1682 (0.1675)  loss_box_reg: 0.1650 (0.1600)  loss_objectness: 0.0346 (0.0312)  loss_rpn_box_reg: 0.0565 (0.0542)  time: 0.5111  data: 0.0112  max mem: 6482\n",
      "Epoch: [3]  [2400/2699]  eta: 0:02:33  lr: 0.000500  loss: 0.3973 (0.4123)  loss_classifier: 0.1669 (0.1673)  loss_box_reg: 0.1394 (0.1597)  loss_objectness: 0.0234 (0.0311)  loss_rpn_box_reg: 0.0496 (0.0541)  time: 0.5316  data: 0.0148  max mem: 6482\n",
      "Epoch: [3]  [2500/2699]  eta: 0:01:42  lr: 0.000500  loss: 0.3976 (0.4121)  loss_classifier: 0.1682 (0.1672)  loss_box_reg: 0.1426 (0.1596)  loss_objectness: 0.0329 (0.0311)  loss_rpn_box_reg: 0.0568 (0.0541)  time: 0.5208  data: 0.0118  max mem: 6482\n",
      "Epoch: [3]  [2600/2699]  eta: 0:00:50  lr: 0.000500  loss: 0.3840 (0.4121)  loss_classifier: 0.1565 (0.1672)  loss_box_reg: 0.1530 (0.1597)  loss_objectness: 0.0240 (0.0312)  loss_rpn_box_reg: 0.0442 (0.0541)  time: 0.5115  data: 0.0106  max mem: 6482\n",
      "Epoch: [3]  [2698/2699]  eta: 0:00:00  lr: 0.000500  loss: 0.4316 (0.4118)  loss_classifier: 0.1695 (0.1670)  loss_box_reg: 0.1652 (0.1595)  loss_objectness: 0.0297 (0.0311)  loss_rpn_box_reg: 0.0570 (0.0541)  time: 0.4946  data: 0.0104  max mem: 6482\n",
      "Epoch: [3] Total time: 0:23:03 (0.5128 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:12:51  model_time: 0.2169 (0.2169)  evaluator_time: 0.2700 (0.2700)  time: 1.1427  data: 0.6352  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:04:51  model_time: 0.1490 (0.1521)  evaluator_time: 0.3161 (0.3294)  time: 0.4876  data: 0.0112  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:04:55  model_time: 0.1491 (0.1509)  evaluator_time: 0.7414 (0.4488)  time: 0.8835  data: 0.0108  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:34  model_time: 0.1526 (0.1521)  evaluator_time: 0.2738 (0.3992)  time: 0.5325  data: 0.0104  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:29  model_time: 0.1475 (0.1520)  evaluator_time: 0.4302 (0.3712)  time: 0.6092  data: 0.0104  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:33  model_time: 0.1510 (0.1517)  evaluator_time: 0.1636 (0.3591)  time: 0.5988  data: 0.0119  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:41  model_time: 0.1599 (0.1516)  evaluator_time: 0.0847 (0.3863)  time: 0.3576  data: 0.0105  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1550 (0.1518)  evaluator_time: 0.0709 (0.3692)  time: 0.2464  data: 0.0098  max mem: 6482\n",
      "Test: Total time: 0:06:05 (0.5417 s / it)\n",
      "Averaged stats: model_time: 0.1550 (0.1518)  evaluator_time: 0.0709 (0.3692)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.63s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.461\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.899\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.415\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.039\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.452\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.521\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.016\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.147\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.531\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.128\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.522\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.586\n",
      "Epoch: [4]  [   0/2699]  eta: 1:08:26  lr: 0.000500  loss: 0.4021 (0.4021)  loss_classifier: 0.1715 (0.1715)  loss_box_reg: 0.1631 (0.1631)  loss_objectness: 0.0273 (0.0273)  loss_rpn_box_reg: 0.0401 (0.0401)  time: 1.5216  data: 0.8287  max mem: 6482\n",
      "Epoch: [4]  [ 100/2699]  eta: 0:25:19  lr: 0.000500  loss: 0.3637 (0.4094)  loss_classifier: 0.1458 (0.1655)  loss_box_reg: 0.1350 (0.1573)  loss_objectness: 0.0237 (0.0314)  loss_rpn_box_reg: 0.0475 (0.0552)  time: 0.5129  data: 0.0116  max mem: 6482\n",
      "Epoch: [4]  [ 200/2699]  eta: 0:22:52  lr: 0.000500  loss: 0.3993 (0.4103)  loss_classifier: 0.1785 (0.1664)  loss_box_reg: 0.1615 (0.1580)  loss_objectness: 0.0215 (0.0308)  loss_rpn_box_reg: 0.0509 (0.0552)  time: 0.5180  data: 0.0108  max mem: 6482\n",
      "Epoch: [4]  [ 300/2699]  eta: 0:21:25  lr: 0.000500  loss: 0.4341 (0.4117)  loss_classifier: 0.1694 (0.1665)  loss_box_reg: 0.1627 (0.1590)  loss_objectness: 0.0247 (0.0309)  loss_rpn_box_reg: 0.0526 (0.0553)  time: 0.5129  data: 0.0107  max mem: 6482\n",
      "Epoch: [4]  [ 400/2699]  eta: 0:20:18  lr: 0.000500  loss: 0.3541 (0.4081)  loss_classifier: 0.1539 (0.1661)  loss_box_reg: 0.1255 (0.1566)  loss_objectness: 0.0228 (0.0302)  loss_rpn_box_reg: 0.0564 (0.0551)  time: 0.5089  data: 0.0106  max mem: 6482\n",
      "Epoch: [4]  [ 500/2699]  eta: 0:19:17  lr: 0.000500  loss: 0.3384 (0.4038)  loss_classifier: 0.1372 (0.1643)  loss_box_reg: 0.1398 (0.1551)  loss_objectness: 0.0206 (0.0298)  loss_rpn_box_reg: 0.0444 (0.0545)  time: 0.5085  data: 0.0106  max mem: 6482\n",
      "Epoch: [4]  [ 600/2699]  eta: 0:18:19  lr: 0.000500  loss: 0.3880 (0.4008)  loss_classifier: 0.1611 (0.1633)  loss_box_reg: 0.1477 (0.1541)  loss_objectness: 0.0234 (0.0294)  loss_rpn_box_reg: 0.0538 (0.0540)  time: 0.5104  data: 0.0109  max mem: 6482\n",
      "Epoch: [4]  [ 700/2699]  eta: 0:17:23  lr: 0.000500  loss: 0.3806 (0.4003)  loss_classifier: 0.1488 (0.1631)  loss_box_reg: 0.1458 (0.1539)  loss_objectness: 0.0228 (0.0292)  loss_rpn_box_reg: 0.0467 (0.0541)  time: 0.5120  data: 0.0112  max mem: 6482\n",
      "Epoch: [4]  [ 800/2699]  eta: 0:16:29  lr: 0.000500  loss: 0.3787 (0.3993)  loss_classifier: 0.1570 (0.1626)  loss_box_reg: 0.1531 (0.1537)  loss_objectness: 0.0280 (0.0293)  loss_rpn_box_reg: 0.0493 (0.0538)  time: 0.5111  data: 0.0106  max mem: 6482\n",
      "Epoch: [4]  [ 900/2699]  eta: 0:15:35  lr: 0.000500  loss: 0.3553 (0.3987)  loss_classifier: 0.1435 (0.1622)  loss_box_reg: 0.1413 (0.1538)  loss_objectness: 0.0263 (0.0290)  loss_rpn_box_reg: 0.0467 (0.0537)  time: 0.5156  data: 0.0125  max mem: 6482\n",
      "Epoch: [4]  [1000/2699]  eta: 0:14:41  lr: 0.000500  loss: 0.3631 (0.3977)  loss_classifier: 0.1495 (0.1619)  loss_box_reg: 0.1289 (0.1533)  loss_objectness: 0.0255 (0.0290)  loss_rpn_box_reg: 0.0481 (0.0535)  time: 0.5186  data: 0.0111  max mem: 6482\n",
      "Epoch: [4]  [1100/2699]  eta: 0:13:48  lr: 0.000500  loss: 0.3750 (0.3966)  loss_classifier: 0.1488 (0.1616)  loss_box_reg: 0.1463 (0.1529)  loss_objectness: 0.0217 (0.0287)  loss_rpn_box_reg: 0.0492 (0.0533)  time: 0.5113  data: 0.0107  max mem: 6482\n",
      "Epoch: [4]  [1200/2699]  eta: 0:12:56  lr: 0.000500  loss: 0.3914 (0.3972)  loss_classifier: 0.1637 (0.1618)  loss_box_reg: 0.1581 (0.1534)  loss_objectness: 0.0262 (0.0287)  loss_rpn_box_reg: 0.0551 (0.0533)  time: 0.5074  data: 0.0105  max mem: 6482\n",
      "Epoch: [4]  [1300/2699]  eta: 0:12:03  lr: 0.000500  loss: 0.3869 (0.3967)  loss_classifier: 0.1637 (0.1616)  loss_box_reg: 0.1531 (0.1533)  loss_objectness: 0.0258 (0.0285)  loss_rpn_box_reg: 0.0531 (0.0533)  time: 0.5101  data: 0.0108  max mem: 6482\n",
      "Epoch: [4]  [1400/2699]  eta: 0:11:11  lr: 0.000500  loss: 0.3564 (0.3961)  loss_classifier: 0.1499 (0.1612)  loss_box_reg: 0.1517 (0.1530)  loss_objectness: 0.0216 (0.0286)  loss_rpn_box_reg: 0.0421 (0.0533)  time: 0.5070  data: 0.0105  max mem: 6482\n",
      "Epoch: [4]  [1500/2699]  eta: 0:10:19  lr: 0.000500  loss: 0.3324 (0.3962)  loss_classifier: 0.1467 (0.1612)  loss_box_reg: 0.1458 (0.1531)  loss_objectness: 0.0253 (0.0286)  loss_rpn_box_reg: 0.0410 (0.0534)  time: 0.5093  data: 0.0104  max mem: 6482\n",
      "Epoch: [4]  [1600/2699]  eta: 0:09:27  lr: 0.000500  loss: 0.3883 (0.3958)  loss_classifier: 0.1521 (0.1609)  loss_box_reg: 0.1501 (0.1530)  loss_objectness: 0.0248 (0.0285)  loss_rpn_box_reg: 0.0554 (0.0533)  time: 0.5275  data: 0.0141  max mem: 6482\n",
      "Epoch: [4]  [1700/2699]  eta: 0:08:35  lr: 0.000500  loss: 0.3998 (0.3951)  loss_classifier: 0.1695 (0.1605)  loss_box_reg: 0.1603 (0.1530)  loss_objectness: 0.0200 (0.0284)  loss_rpn_box_reg: 0.0506 (0.0531)  time: 0.5149  data: 0.0109  max mem: 6482\n",
      "Epoch: [4]  [1800/2699]  eta: 0:07:43  lr: 0.000500  loss: 0.3830 (0.3951)  loss_classifier: 0.1591 (0.1605)  loss_box_reg: 0.1614 (0.1531)  loss_objectness: 0.0229 (0.0284)  loss_rpn_box_reg: 0.0445 (0.0531)  time: 0.5107  data: 0.0109  max mem: 6482\n",
      "Epoch: [4]  [1900/2699]  eta: 0:06:52  lr: 0.000500  loss: 0.3390 (0.3951)  loss_classifier: 0.1391 (0.1607)  loss_box_reg: 0.1323 (0.1531)  loss_objectness: 0.0232 (0.0283)  loss_rpn_box_reg: 0.0432 (0.0530)  time: 0.5096  data: 0.0103  max mem: 6482\n",
      "Epoch: [4]  [2000/2699]  eta: 0:06:00  lr: 0.000500  loss: 0.3769 (0.3953)  loss_classifier: 0.1608 (0.1607)  loss_box_reg: 0.1612 (0.1532)  loss_objectness: 0.0247 (0.0284)  loss_rpn_box_reg: 0.0440 (0.0531)  time: 0.5081  data: 0.0104  max mem: 6482\n",
      "Epoch: [4]  [2100/2699]  eta: 0:05:08  lr: 0.000500  loss: 0.3699 (0.3954)  loss_classifier: 0.1611 (0.1606)  loss_box_reg: 0.1581 (0.1532)  loss_objectness: 0.0261 (0.0285)  loss_rpn_box_reg: 0.0481 (0.0531)  time: 0.5096  data: 0.0109  max mem: 6482\n",
      "Epoch: [4]  [2200/2699]  eta: 0:04:17  lr: 0.000500  loss: 0.3898 (0.3944)  loss_classifier: 0.1575 (0.1602)  loss_box_reg: 0.1555 (0.1531)  loss_objectness: 0.0226 (0.0283)  loss_rpn_box_reg: 0.0493 (0.0529)  time: 0.5104  data: 0.0107  max mem: 6482\n",
      "Epoch: [4]  [2300/2699]  eta: 0:03:25  lr: 0.000500  loss: 0.3736 (0.3938)  loss_classifier: 0.1601 (0.1599)  loss_box_reg: 0.1499 (0.1528)  loss_objectness: 0.0210 (0.0282)  loss_rpn_box_reg: 0.0524 (0.0528)  time: 0.5285  data: 0.0120  max mem: 6482\n",
      "Epoch: [4]  [2400/2699]  eta: 0:02:34  lr: 0.000500  loss: 0.3461 (0.3932)  loss_classifier: 0.1441 (0.1597)  loss_box_reg: 0.1452 (0.1526)  loss_objectness: 0.0171 (0.0281)  loss_rpn_box_reg: 0.0426 (0.0528)  time: 0.5087  data: 0.0105  max mem: 6482\n",
      "Epoch: [4]  [2500/2699]  eta: 0:01:42  lr: 0.000500  loss: 0.4186 (0.3927)  loss_classifier: 0.1696 (0.1596)  loss_box_reg: 0.1651 (0.1525)  loss_objectness: 0.0201 (0.0280)  loss_rpn_box_reg: 0.0552 (0.0527)  time: 0.5080  data: 0.0102  max mem: 6482\n",
      "Epoch: [4]  [2600/2699]  eta: 0:00:50  lr: 0.000500  loss: 0.4445 (0.3934)  loss_classifier: 0.1764 (0.1598)  loss_box_reg: 0.1634 (0.1527)  loss_objectness: 0.0353 (0.0281)  loss_rpn_box_reg: 0.0501 (0.0527)  time: 0.5113  data: 0.0109  max mem: 6482\n",
      "Epoch: [4]  [2698/2699]  eta: 0:00:00  lr: 0.000500  loss: 0.3808 (0.3935)  loss_classifier: 0.1524 (0.1598)  loss_box_reg: 0.1476 (0.1528)  loss_objectness: 0.0295 (0.0282)  loss_rpn_box_reg: 0.0528 (0.0527)  time: 0.4953  data: 0.0112  max mem: 6482\n",
      "Epoch: [4] Total time: 0:23:09 (0.5149 s / it)\n",
      "creating index...\n",
      "index created!\n",
      "Test:  [  0/675]  eta: 0:17:42  model_time: 0.1842 (0.1842)  evaluator_time: 0.2530 (0.2530)  time: 1.5739  data: 1.1137  max mem: 6482\n",
      "Test:  [100/675]  eta: 0:04:44  model_time: 0.1477 (0.1509)  evaluator_time: 0.2774 (0.3132)  time: 0.4390  data: 0.0106  max mem: 6482\n",
      "Test:  [200/675]  eta: 0:04:52  model_time: 0.1481 (0.1507)  evaluator_time: 0.7481 (0.4394)  time: 0.8873  data: 0.0112  max mem: 6482\n",
      "Test:  [300/675]  eta: 0:03:31  model_time: 0.1506 (0.1517)  evaluator_time: 0.2664 (0.3900)  time: 0.4916  data: 0.0104  max mem: 6482\n",
      "Test:  [400/675]  eta: 0:02:28  model_time: 0.1470 (0.1516)  evaluator_time: 0.4094 (0.3646)  time: 0.5992  data: 0.0105  max mem: 6482\n",
      "Test:  [500/675]  eta: 0:01:32  model_time: 0.1515 (0.1515)  evaluator_time: 0.1426 (0.3533)  time: 0.6021  data: 0.0118  max mem: 6482\n",
      "Test:  [600/675]  eta: 0:00:41  model_time: 0.1592 (0.1513)  evaluator_time: 0.0813 (0.3804)  time: 0.3519  data: 0.0101  max mem: 6482\n",
      "Test:  [674/675]  eta: 0:00:00  model_time: 0.1557 (0.1515)  evaluator_time: 0.0792 (0.3636)  time: 0.2576  data: 0.0103  max mem: 6482\n",
      "Test: Total time: 0:06:02 (0.5365 s / it)\n",
      "Averaged stats: model_time: 0.1557 (0.1515)  evaluator_time: 0.0792 (0.3636)\n",
      "Accumulating evaluation results...\n",
      "DONE (t=1.53s).\n",
      "IoU metric: bbox\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.467\n",
      " Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.900\n",
      " Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.425\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.040\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.457\n",
      " Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.522\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.016\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.148\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.536\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.122\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.529\n",
      " Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.583\n"
     ]
    }
   ],
   "source": [
    "num_classes = 2\n",
    "train_dataset = WheatDataset(train_df, folds=[0, 1, 2, 3])\n",
    "train_loader = data_utils.DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4, collate_fn=utils.collate_fn)\n",
    "\n",
    "val_dataset = WheatDataset(train_df, folds=[4])\n",
    "val_loader = data_utils.DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4, collate_fn=utils.collate_fn)\n",
    "\n",
    "\n",
    "# move model to the right device\n",
    "model.to(DEVICE)\n",
    "\n",
    "# construct an optimizer\n",
    "params = [p for p in model.parameters() if p.requires_grad]\n",
    "optimizer = torch.optim.SGD(params, lr=0.005, momentum=0.9, weight_decay=0.0005)\n",
    "\n",
    "# and a learning rate scheduler\n",
    "lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1)\n",
    "\n",
    "for epoch in range(N_EPOCHS):\n",
    "    # train for one epoch, printing every 100 iterations\n",
    "    train_one_epoch(model, optimizer, train_loader, DEVICE, epoch, print_freq=100)\n",
    "    # update the learning rate\n",
    "    lr_scheduler.step()\n",
    "    # evaluate on the validation dataset\n",
    "    evaluate(model, val_loader, device=DEVICE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm -rf *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "torch.save(model.state_dict(), 'fasterrcnn_resnet101_fold4.pth')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_bbox(bboxes, col, color='white'):\n",
    "    \n",
    "    for i in range(len(bboxes)):\n",
    "        # Create a Rectangle patch\n",
    "        rect = patches.Rectangle(\n",
    "            (bboxes[i][0], bboxes[i][1]),\n",
    "            bboxes[i][2] - bboxes[i][0], \n",
    "            bboxes[i][3] - bboxes[i][1], \n",
    "            linewidth=2, \n",
    "            edgecolor=color, \n",
    "            facecolor='none')\n",
    "\n",
    "        # Add the patch to the Axes\n",
    "        col.add_patch(rect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'device' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-14-e23ba2552ea7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      3\u001b[0m     \u001b[0mimage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcvtColor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCOLOR_BGR2RGB\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m     \u001b[0mimage\u001b[0m \u001b[0;34m/=\u001b[0m \u001b[0;36m255.0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m     \u001b[0mpreds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_numpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtranspose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdevice\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m     \u001b[0mpred_bboxes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpreds\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'boxes'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcpu\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdetach\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'device' is not defined"
     ]
    }
   ],
   "source": [
    "for img in os.listdir(TEST_DIR)[:5]:\n",
    "    image = cv2.imread(os.path.join(TEST_DIR, img), cv2.IMREAD_COLOR)\n",
    "    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n",
    "    image /= 255.0\n",
    "    preds = model([torch.from_numpy(np.transpose(image, (2, 0, 1))).to(device)])[0]\n",
    "    \n",
    "    pred_bboxes = preds['boxes'].cpu().detach().numpy()\n",
    "    pred_scores = preds['scores'].cpu().detach().numpy()\n",
    "    \n",
    "    mask = pred_scores >= 0.4\n",
    "    pred_scores = pred_scores[mask]\n",
    "    pred_bboxes = pred_bboxes[mask]\n",
    "    \n",
    "    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 10))\n",
    "    get_bbox(pred_bboxes, ax, color='red')\n",
    "    ax.imshow(image)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {
     "0616a8eb89384b8f824236a0b0987d44": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "HTMLModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "HTMLView",
       "description": "",
       "description_tooltip": null,
       "layout": "IPY_MODEL_a6cd75389db3422aba092fa549777590",
       "placeholder": "​",
       "style": "IPY_MODEL_0b31d9a5d5284e5da5f379cce09b8e98",
       "value": " 170M/170M [00:12&lt;00:00, 13.9MB/s]"
      }
     },
     "0b31d9a5d5284e5da5f379cce09b8e98": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "DescriptionStyleModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "StyleView",
       "description_width": ""
      }
     },
     "165ee518608f472c962d60b3869b041c": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HBoxModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "HBoxModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "HBoxView",
       "box_style": "",
       "children": [
        "IPY_MODEL_cb1183b546284f87b5db603b5df84dcc",
        "IPY_MODEL_0616a8eb89384b8f824236a0b0987d44"
       ],
       "layout": "IPY_MODEL_25679a334a204793bf6feb6e96c9c15b"
      }
     },
     "25679a334a204793bf6feb6e96c9c15b": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "561c1cee7eba4f058bc264776c1fa4ab": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "9ee82b22a31948c281fdacee95d4c3d3": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "ProgressStyleModel",
      "state": {
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "ProgressStyleModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "StyleView",
       "bar_color": null,
       "description_width": "initial"
      }
     },
     "a6cd75389db3422aba092fa549777590": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "cb1183b546284f87b5db603b5df84dcc": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "FloatProgressModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "FloatProgressModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "ProgressView",
       "bar_style": "success",
       "description": "100%",
       "description_tooltip": null,
       "layout": "IPY_MODEL_561c1cee7eba4f058bc264776c1fa4ab",
       "max": 178728960.0,
       "min": 0.0,
       "orientation": "horizontal",
       "style": "IPY_MODEL_9ee82b22a31948c281fdacee95d4c3d3",
       "value": 178728960.0
      }
     }
    },
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
